package com.zxy.fmmall.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zxy.fmmall.dao.CategoryMapper;
import com.zxy.fmmall.entity.Category;
import com.zxy.fmmall.service.CategoryService;
import com.zxy.fmmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ResultVo selectAllCategories() {
        List<Category> res = null;
        String resStr = stringRedisTemplate.boundValueOps("res").get();
        try {
            if (resStr != null) {
                JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, Category.class);
                res = objectMapper.readValue(resStr, javaType);
            } else {
                // 防止缓存击穿   加锁
                synchronized (this) {
                    //再次从redis中取数据
                    String s = stringRedisTemplate.boundValueOps("res").get();
                    // 如果没有数据取数据库取数据并放到redis
                    if (s == null) {
                        res = categoryMapper.selectAllCategories(0);
                        if (res == null){
                            ArrayList<Category> categories = new ArrayList<>();
                            stringRedisTemplate.boundValueOps("res").set(objectMapper.writeValueAsString(categories),10, TimeUnit.SECONDS);
                                //也可以这样写
                                //stringRedisTemplate.boundValueOps("res").set("[]"),10, TimeUnit.SECONDS);
                        }else {
                            stringRedisTemplate.boundValueOps("res").set(objectMapper.writeValueAsString(res),1, TimeUnit.DAYS);
                        }
                    } else {
                        //将redis中数据返回
                        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, Category.class);
                        res = objectMapper.readValue(s, javaType);
                    }
                }

            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return ResultVo.ok().data("res", res);
    }

    @Override
    public ResultVo selectFirstLevelCategories() {
        List<Category> categories = categoryMapper.selectFirstLevelCategories();
        return ResultVo.ok().data("popularProduct", categories);
    }

//    @Override
//    public ResultVo selectAllCategories() {
//        List<Category> categories = categoryMapper.selectAll();
//        List<Category> res = selectParentCategory(categories);
//        return  ResultVo.ok().data("res",res);
//
//    }
//    public List<Category> selectParentCategory(List<Category> categories){
//        ArrayList<Category> res = new ArrayList<>();
//        for (Category category : categories) {
//            if (category.getParentId() == 0){
//                res.add(selectChildrenCategory(categories,category));
//            }
//        }
//        return res;
//    }
//    public Category selectChildrenCategory(List<Category> categories,Category category){
//        category.setCategoryList(new ArrayList<Category>());
//        for (Category category1 : categories) {
//            if (category1.getParentId() == category.getCategoryId()){
////                Category category2 = selectChildrenCategory(categories, category1);
////                category.getCategoryList().add(category2);
//                category.getCategoryList().add(selectChildrenCategory(categories,category1));
//            }
//        }
//        return category;
//    }
}
